package com.dragon.dragonshop.service.impl;

import com.dragon.dragonshop.dao.UserMapper;
import com.dragon.dragonshop.pojo.User;
import com.dragon.dragonshop.pojo.UserExample;
import com.dragon.dragonshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public boolean checkUser(User loginUser) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(loginUser.getUserName());
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList!=null){
            if (userList.get(0).getPassword().equals(loginUser.getPassword())){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean hasUser(String userName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<User> userList = userMapper.selectByExample(userExample);

        if (userList.size()>0)
            return true;
        return false;
    }

    @Override
    @Transactional
    public boolean addUser(User user) {
        int insert = userMapper.insert(user);
        if(insert>0)
            return  true;

        return false;
    }
}
